package com.cloud.gatewaySpringSecurity.handler;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authentication.ServerFormLoginAuthenticationConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author zhozixiong
 * @date 2023/03/02
 *
 * 密码登录参数处理器
 */
@Component
public class PasswordHandler extends ServerFormLoginAuthenticationConverter {

    @Override
    public Mono<Authentication> convert(ServerWebExchange exchange) {
        MultiValueMap<String, String> map = exchange.getRequest().getQueryParams();
        String username = map.get("username").get(0);
        String password = map.get("password").get(0);
        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }
        UsernamePasswordAuthenticationToken usernamePasswordToken = new UsernamePasswordAuthenticationToken(username, password);
        return Mono.just(usernamePasswordToken);
    }
}
